/**
 * JAVACC DEMO 1.0
 */
package com.apache.uct.service.plugins;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.cache.util.Validator;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.database.model.Page;
import com.apache.uct.common.entity.Act;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * description:  获取权限列表插件
 *
 * @author iussoft 创建时间：2016-3-26
 */
public class ActListPlugin implements PluginConnector {

    private Logger log = LoggerFactory.getLogger(ActListPlugin.class);

    private IDao actDao;

    /**
     * TODO 获取权限列表插件.<br>
     * 参数说明:<br>
     * userId:查询指定用户的权限列表;<br>
     * roleId:查询指定角色的权限列表;<br>
     * act属性:按权限属性查询权限列表;<br>
     * 分页设置<br>
     * pageSize:每页调数; pageIndex:当前页码
     */
    public Object execute(ParamsVo vo) throws Exception {
        ResultEntity result = new ResultEntity();
        try {
            result.setResult("true");
            String userId = String.valueOf(vo.getParams("userId"));
            String roleId = String.valueOf(vo.getParams("roleId"));
            int pageSize = Integer.valueOf(Validator
                    .getDefaultStr(String.valueOf(vo.getParams("pageSize")),
                            Validator.getDefaultStr(String.valueOf(vo.getParams("rows")), "0")));
            int pageIndex = Integer.valueOf(Validator
                    .getDefaultStr(String.valueOf(vo.getParams("pageIndex")),
                            Validator.getDefaultStr(String.valueOf(vo.getParams("page")), "0")));
            boolean isPage = false;
            if (pageSize > 0 && pageIndex > 0)
                isPage = true;
            if (Validator.isNotNull(roleId) || Validator.isNotNull(userId)) {
                MethodParam param = new MethodParam("ByAllUserId", "", actDao.getSql(5),
                        Act.class.getName());
                if (Validator.isNotNull(String.valueOf(vo.getParams("actType")))) {
                    param.setParams("actType", String.valueOf(vo.getParams("actType")));
                }
                if (isPage) {
                    param.setPageIndex(pageIndex);
                    param.setPageSize(pageSize);
                    if (Validator.isNotNull(userId)) {
                        param.setParams("userId", userId);
                    } else {
                        param.setKey("ByRoleId");
                        param.setSqlStr(actDao.getSql(6) + " and roleId ='" + roleId + "') )");
                        param.setParams("roleId", roleId);
                        param.setOrderby(" full_Ename ");
                    }
                    param.setResultCache(false);
                    result.setEntity(actDao.select(param));
                } else {
                    if (Validator.isNotNull(userId)) {
                        param.setParams("userId", userId);
                    } else {
                        param.setKey("ByRoleId");
                        param.setSqlStr(actDao.getSql(6) + " and roleId ='" + roleId + "') )");
                        param.setParams("roleId", roleId);
                        param.setOrderby(" full_Ename ");
                    }
                    param.setResultCache(false);
                    result.setEntity(actDao.select(param));
                }
            } else {//按用户ID或角色ID获取权限列表
                MethodParam param = setMethodParams(vo, 2);
                if (Validator.isEmpty(param)) {
                    result.setMessage("查询条件为空或不存在相关信息");
                    return result;
                }
                if (isPage) {
                    param.setPageIndex(pageIndex);
                    param.setPageSize(pageSize);
                    param.setOrderby(" order_Num asc,create_Time desc");
                    Page page = actDao.pageSelect(param);
                    result.setEntity(page);
                } else {
                    param.setOrderby(" order_Num asc,create_Time desc");
                    result.setEntity(actDao.select(param));
                }
            }
        } catch (Exception e) {
            result.setResult("false");
            log.error("执行机构查询失败 ", e);
            result.setMessage("机构查询失败！");
        }
        return result;
    }

    private MethodParam setMethodParams(ParamsVo<Act> vo, int type) {
        StringBuffer sb = new StringBuffer(actDao.getSql(type));
        MethodParam param = new MethodParam("ByProperty", "", "", Act.class.getName());
        String str = buildParams(param, vo);
        if (Validator.isNull(str)) {
            return null;
        }
        sb.append(str);

        sb.append(" and actStatus = :actStatus");
        param.setParams("actStatus", "1");
        sb.append(" order by orderNum asc,createTime desc");
        param.setSqlStr(sb.toString());
        return param;
    }

    private String buildParams(MethodParam param, ParamsVo vo) {
        StringBuffer sb = new StringBuffer();
        if (Validator.isNotNull(String.valueOf(vo.getParams("sysEname")))) {
            sb.append(" and sysEname = :sysEname");
            param.setParams("sysEname", String.valueOf(vo.getParams("sysEname")));
        }
        if (Validator.isNotNull(String.valueOf(vo.getParams("actEname")))) {
            sb.append(" and actEname = :actEname");
            param.setParams("actEname", String.valueOf(vo.getParams("actEname")));
        }
        if (Validator.isNotNull(String.valueOf(vo.getParams("actCname")))) {
            sb.append(" and actCname like :actCname");
            param.setParams("actCnames", "'%" + String.valueOf(vo.getParams("actCname")) + "%'");
        }
        if (Validator.isNotNull(String.valueOf(vo.getParams("fullEname")))) {
            sb.append(" and fullEname = :fullEname");
            param.setParams("fullEname", String.valueOf(vo.getParams("fullEname")));
        }
        if (Validator.isNotNull(String.valueOf(vo.getParams("fullCname")))) {
            sb.append(" and fullCname like :fullCname");
            param.setParams("fullCnames", "'%" + String.valueOf(vo.getParams("fullCname")) + "%'");
        }
        if (Validator.isNotNull(String.valueOf(vo.getParams("actType")))) {
            sb.append(" and actType = :actType");
            param.setParams("actType", String.valueOf(vo.getParams("actType")));
        }
        if (Validator.isNotNull(String.valueOf(vo.getParams("fatherId")))) {
            sb.append(" and fatherId = :fatherId");
            param.setParams("fatherId", String.valueOf(vo.getParams("fatherId")));
        }
        if (Validator.isNotNull(String.valueOf(vo.getParams("actUrl")))) {
            sb.append(" and actUrl = :actUrl");
            param.setParams("actUrl", String.valueOf(vo.getParams("actUrl")));
        }
        if (Validator.isNotNull(String.valueOf(vo.getParams("actRemark")))) {
            sb.append(" and actRemark = :actRemark");
            param.setParams("actRemark", String.valueOf(vo.getParams("actRemark")));
        }
        return sb.toString();
    }

    public void setActDao(IDao actDao) {
        this.actDao = actDao;
    }
}
